<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class User_center extends CI_Controller{
	public function __construct() {
		parent::__construct ();
		$this->tologin->isLogin ( site_url ( 'login' ) );
		$this->load->model("user_model");
		$this->load->model("project_model");
		$this->load->model("category_model");
		$this->load->model("project_participater_model");
		$this->load->model('category_model');
		$whereArr = array("status"=>1);
		$this->category_list = $this->category_model->resultAll($whereArr);
	}
	/*
	 *用户中心 
	 */
	public function index(){
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['index'] = true;
		$data['user_message'] = $user_message;
		$data['category_list'] = $this->category_list;
		$this->load->view("user_center",$data);
	}
	/*
	 *修改资料 
	 */
	public function change_data(){
		$uid = $this->session->userdata('uid');
		$whereArr = array("uid"=>$uid);
		$telephone = $this->input->post("telephone");
		$dataArr = array("telephone"=>$telephone);
		
		$result = $this->user_model->update($dataArr,$whereArr);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "修改个人资料成功!!";
			echo json_encode($message);
		}else{
			$message['flag'] = -1;
			$message['content'] = "修改个人资料失败!!";
			echo json_encode($message);
		}
	}
	/*
	 *显示用户修改密码页面 
	 */
	public function show_change_password(){
		$data['show_change_password'] = true;
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['user_message'] = $user_message;
		$data['category_list'] = $this->category_list;
		$this->load->view("user_change_password",$data);
	}
	/*
	 *修改用户密码
	 */
	public function do_change_password(){
		$password = $this->input->post("password");
		$newpassword = $this->input->post("newpassword");
		$newpassword2 = $this->input->post("newpassword2");
		$oldPassword = $this->findOldPassword();
		if($password != $oldPassword){
			$message['flag'] = -1;
			$message['content'] = "输入的旧密码有误!!";
			$this->message_show($message);
		}
		if($password==$newpassword){
			$message['flag'] = -1;
			$message['content'] = "新密码不能跟原密码一样!!";
			$this->message_show($message);
		}
		if($newpassword != $newpassword2){
			$message["flag"] = -1;
			$message["content"] = "新密码与确认新密码不一致!!";
			$this->message_show($message);
		}
		$uid = $this->session->userdata('uid');
		$whereArr = array("uid"=>$uid,"status"=>1);
		$dataArr = array("password"=>$this->_encrypt($newpassword));
		$result = $this->user_model->update($dataArr,$whereArr);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "修改密码成功!!";
			$message['url'] = site_url('Logout');
			$this->message_show($message);
		}else{
			$message['flag'] = -1;
			$message['content'] = "修改密码失败!!";
			$this->message_show($message);
		}
	}
	/*
	 *加载更换用户头像 页面
	 */
	public function user_change_logo(){
		$data['user_change_logo'] = true;
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['user_message'] = $user_message;
		$data['category_list'] = $this->category_list;
		$this->load->view("user_change_logo",$data);
	}
	/*
	 *执行更改头像操作 
	 */
	public function do_user_change_logo(){
		$user_logo = $this->input->post("user_logo");
		if(empty($user_logo)){
			$message['flag'] = -1;
			$message['content'] = "别闹，你还没有上传图片呢!";
			$this->message_show($message); 
		}
		$uid = $this->session->userdata('uid');
		$whereArr = array("uid" => $uid);
		$dataArr = array("logo"=>$user_logo);
		$result = $this->user_model->update($dataArr, $whereArr);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "更改头像成功!";
			$message['url'] = site_url('User_center');
			$this->message_show($message); 
		}else{
			$message['flag'] = -1;
			$message['content'] = "更新图片失败!";
			$this->message_show($message);
		}
	}
	/*
	 *我发起的项目 
	 */
	public function my_create_project(){
		$data['my_create_project'] = true;
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['user_message'] = $user_message;
		
		$uid = $this->session->userdata('uid');
		$whereArr = array("uid"=>$uid,"status"=>1);
		$project_list = $this->project_model->resultAll($whereArr,"starttime@DESC");
		foreach ($project_list as $key => $value){
			$category = $this->change_cid_to_cname($value['cid']);
			$project_list[$key]['cname'] = $category['cname'];
			$project_list[$key]['join_count'] = $this->join_project_num($value['pid']);
			$project_list[$key]['isEnd'] = $this->count_day($value['starttime'], $value['addtime']);
			$project_list[$key]['logo'] = ltrim($value['logo'],".");
		}
		$data['project_list'] = $project_list;
		$data['category_list'] = $this->category_list;
		$this->load->view("my_create_project",$data);
	}
	/*
	 *删除项目
	 */
	public function delete_project($pid=1){
		$whereArr = array("pid"=>$pid);
		$dataArr = array("status"=>0);
		$result = $this->project_model->update($dataArr,$whereArr);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "项目删除成功!";
			$this->message_show($message);
		}else{
			$message['flag'] = -1;
			$message['content'] = "项目删除失败!";
			$this->message_show($message);
		}
	}
	/*
	 *我参加的项目 
	 */
	public function my_join_project(){
		$data['my_join_project'] = true;
		$user_message = $this->user_message();
		$user_message['logo'] = ltrim($user_message['logo'],".");
		$data['user_message'] = $user_message;
		
		$uid = $this->session->userdata('uid');
		$whereArr = array("uid"=>$uid,'status'=>1);
		
		$my_join_project_list = $this->project_participater_model->resultAll($whereArr,"createtime@DESC");
		$i=0;
		foreach($my_join_project_list as $key => $value){
			$project_flag = $this->find_project_message($value['pid']);
			if($project_flag){
				$project_list[$i] = $project_flag;
				$i++; 
			}
		}
		if(isset($project_list)){
			$data['project_list'] = $project_list;
		}
		$data['category_list'] = $this->category_list;
		$this->load->view("my_join_project",$data);
	}
	/*
	 *放弃参加项目功能 
	 */
	public function abandon_project($pid=1){
		$uid=$this->session->userdata('uid');
		$whereArr = array('pid'=>$pid,'uid'=>$uid);
		$dataArr = array("status"=>0);
		$result = $this->project_participater_model->update($dataArr,$whereArr);
		if($result){
			$message['flag'] = 1;
			$message['content'] = "放弃参加此项目成功!";
			$this->message_show($message);
		}else{
			$message['flag'] = -1;
			$message['content'] = "放弃参加此项目失败!";
			$this->message_show($message);
		}
	}
	/*
	 *查看项目信息情况
	 */
	public function find_project_message($pid){
		$whereArr = array("pid"=>$pid,"status"=>1);
		$project = $this->project_model->row($whereArr);
		if($project){
			$category = $this->change_cid_to_cname($project['cid']);
			$project['cname'] = $category['cname'];
			$project['join_count'] = $this->join_project_num($project['pid']);
			$project['isEnd'] = $this->count_day($project['starttime'], $project['addtime']);
			$project['logo'] = ltrim($project['logo'],".");
			return $project;
		}else{
			return false;
		}
	}
	/*
	 *计算项目剩余天数
	 */
	public function count_day($starttime,$addtime){
		$nowtime = time();
		$time_difference_day = round(($nowtime - $starttime)/3600/24);
		if($time_difference_day<$addtime){
			return ($addtime-$time_difference_day);
		}else{
			return false;
		}
	}
	/*
	 *根据分类id查找分类名数据
	 */
	public function change_cid_to_cname($cid){
		$whereArr = array("cid"=>$cid,"status"=>1);
		$category = $this->category_model->row($whereArr);
		if($category){
			return $category;
		}
	}
	/*
	 *查找项目参加的人数 
	 */
	public function join_project_num($pid){
		$whereArr=array('pid'=>$pid,"status"=>1);
		$count = $this->project_participater_model->count($whereArr);
		return $count;
	}
	/*
	 *消息提示 
	 */
	public function message_show($message){
		echo json_encode($message);
		exit;
	}
	/*
	 *查找用户信息 
	 */
	public function user_message(){
		$uid=$this->session->userdata('uid');
		$whereArr = array('uid'=>$uid);
		$user = $this->user_model->row($whereArr);
		if($user){
			return $user;
		}
	}
	/*
	 *查找原密码 
	 */
	public function findOldPassword(){
		$username = $this->session->userdata('username');
		$whereArr = array("username"=>$username,"status"=>1);
		$user = $this->user_model->row($whereArr);
		if($user){
			$oldPassword = $this->_decrypt($user['password']);
			return $oldPassword;
		}
	}
	/**
	 * 密码加密
	 */
	private function _encrypt($str) {
		$this->load->library ( 'encrypt' );
		$re = $this->encrypt->encode ( $str );
		return $re;
	}
	/**
	 * 密码解密
	 */
	private function _decrypt($str) {
		$this->load->library ( 'encrypt' );
		$re = $this->encrypt->decode ( $str );
		return $re;
	}
}